package top.lvkun.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.RedisUtils;
import top.lvkun.org.domain.Shop;
import top.lvkun.org.mapper.ShopMapper;
import top.lvkun.org.service.IShopService;
import top.lvkun.user.domain.dto.UserDto;
import top.lvkun.user.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/lr")
public class PhoneLoginAndRegController {

    @Autowired
    IUserService userService;
    @Autowired
    IShopService shopService;
    @Autowired
    ShopMapper shopMapper;


    /**
     * 手机号注册
     * @param type
     * @param phone
     * @return
     */
    @GetMapping("/validatePhone/{type}/{phone}")
    public AjaxResult validatePhone(@PathVariable("type") String type,@PathVariable("phone") String phone){
        return userService.validatePhone(phone,type);
    }


    /**
     * 发送手机短信
     * @param type
     * @param phone
     * @return
     */
    @GetMapping("/sendMobileCode/{type}/{phone}")
    public AjaxResult sendMobileCode(@PathVariable("type") String type,@PathVariable("phone") String phone){
        return userService.sendMobileCode(type,phone);
    }


    /**
     * 提交表单，注册用户信息
     * @param userDto
     * @return
     */
    @PutMapping("/phoneReg")
    public AjaxResult phoneReg(@RequestBody UserDto userDto){
        return userService.phoneReg(userDto);
    }


    /**
     * 门店入驻，提交表单
     * @param shop
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop) {
        return shopService.settledIn(shop);
    }


    /**
     * 用户点击邮箱链接，激活门店和个人账号
     * @param id
     * @return
     */
    @GetMapping("/shopRegActive/{id}")
    public void shopRegActive(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        shopService.shopRegActive(id);
        response.setContentType("text/html; charset=UTF-8");//注意text/html，和application/html
        response.getWriter().print("<html><body><script type='text/javascript'>alert('恭喜您！激活成功！点击确认即可跳转后台登录页面！');</script></body></html>");
        response.setHeader("refresh","0;http://172.16.80.248:8081/#/login");
        response.getWriter().close();
    }

    /**
     * 审核未通过，跳转门店入驻页面，并回显信息
     * @param id
     */
    @GetMapping("/reSettleIn/{id}")
    public AjaxResult reSettleIn(@PathVariable("id") Long id){
        try {
            Shop shop = shopMapper.loadById(id);

            return AjaxResult.me().setData(shop);
            //request.getRequestDispatcher("http://172.16.80.248:8081/#/settleIn").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("数据回显失败!");
        }
    }

    /**
     * 用户登录
     * @param userDto
     * @return
     */
    @PostMapping("/userLogin")
    public AjaxResult userLogin(@RequestBody UserDto userDto){
        try {
            return userService.userLogin(userDto);
        } catch (Exception e) {
           e.printStackTrace();
           return AjaxResult.me().setSuccess(false).setMessage("服务繁忙！");
        }
    }

    /**
     * 前后台用户注销
     * @param request
     * @return
     */
    @PostMapping("/userLoginOut")
    public AjaxResult userLoginOut(HttpServletRequest request){
        try {
            String userToken = request.getHeader("userToken");
            if(!StringUtils.isEmpty(userToken)){
                RedisUtils.INSTANCE.del(userToken);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务繁忙！");
        }
    }



    /**
     * 微信登录接口：获取code参数，利用Java程序发送Http请求获取access_token
     * @param code
     * @return
     */
    @GetMapping("/wechatLogin/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        try {
            return userService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登陆失败，我们正在殴打程序员！！");
        }
    }


}
